package com.financialsharing.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.financialsharing.common.ServerResponse;
import com.financialsharing.common.UserUtil;
import com.financialsharing.pojo.exchange.Answer;
import com.financialsharing.pojo.exchange.Question;
import com.financialsharing.service.QuestionAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName ExchangeController
 * @Description TODO  交流中心
 * @Author lvyongwei
 * @Date 2020-12-17 16:38
 **/
@RestController
@RequestMapping("/exchange")
public class ExchangeController {

    @Autowired
    private QuestionAnswerService questionAnswerService;

    /**
     * 做实验时展示的交流中心内容
     * @param question
     * @param request
     * @return
     */
    @PostMapping("/getPageQuestion")
    public ServerResponse getPageQuestion(@RequestBody Question question, HttpServletRequest request){
        /**
         * 解析参数
         */
        Integer pageNum = question.getPageNum();
        Integer pageSize = question.getPageSize();
        if(StringUtils.isEmpty(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Question> questions = questionAnswerService.getPageQuestion();
            PageInfo<Question> pageInfo = new PageInfo<Question>(questions);
            for(Question question1:pageInfo.getList()){
                question1.setAnswers(questionAnswerService.getPageAnswer(question1.getQuestionId(),userId));
            }
            return ServerResponse.createBySuccess("获取内容成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("查询实验报告信息错误!");
        }
    }

    /**
     * 获取提问
     * @param
     * @return
     */
    @PostMapping("/getQuestion")
    public ServerResponse getQuestion(@RequestBody Question question, HttpServletRequest request){
        /**
         * 解析参数
         */
        Integer pageNum = question.getPageNum();
        Integer pageSize = question.getPageSize();
        if(StringUtils.isEmpty(pageNum) || pageNum == 0){
            pageNum = 1;
        }
        if(StringUtils.isEmpty(pageSize) || pageSize == 0){
            pageSize = 10;
        }
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        try{
            PageHelper.startPage(pageNum,pageSize);
            /**
             * 判断是否是教师或管理员
             */
            if(roleId.equals(1) || roleId.equals(2)){
                List<Question> questions = questionAnswerService.listAll(question);
                /**
                 * 获取其他字段
                 */
                for(Question question1:questions){
                    question1.setTotalAnswerCount(questionAnswerService.totalPasswordAnswer(question1.getQuestionId()));
                    question1.setPassAnswerCount(questionAnswerService.getPassAnswer(question1.getQuestionId()));
                }
                PageInfo<Question> pageInfo = new PageInfo<Question>(questions);
                return ServerResponse.createBySuccess("查询成功!",pageInfo);
            }else{
                question.setIfShow(1);
                question.setQuestionUserId(userId);
                List<Question> questions = questionAnswerService.listAll(question);
                /**
                 * 获取其他字段
                 */
                for(Question question1:questions){
                    question1.setTotalAnswerCount(questionAnswerService.totalPasswordAnswer(question1.getQuestionId()));
                    question1.setPassAnswerCount(questionAnswerService.getPassAnswer(question1.getQuestionId()));
                }
                PageInfo<Question> pageInfo = new PageInfo<Question>(questions);
                return ServerResponse.createBySuccess("查询成功!",pageInfo);
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("查询实验报告信息错误!");
        }
    }


    /**
     * 添加提问
     * @param question
     * @return
     */
    @PostMapping("/insertQuestion")
    public ServerResponse insertQuestion(@RequestBody Question question, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        try{
            question.setQuestionUserId(userId);
            question.setIfShow(0);
            //添加操作
            int count = questionAnswerService.insert(question);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("提问成功,审核后才能正常展示!");
            }else{
                return ServerResponse.createByErrorMsg("提问失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("提问失败!");
        }
    }

    /**
     * 修改提问
     * @param question
     * @return
     */
    @PostMapping("/updateQuestion")
    public ServerResponse updateQuestion(@RequestBody Question question, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        question.setRoleId(roleId);
        question.setQuestionUserId(userId);
        try{
            //添加操作
            int count = questionAnswerService.update(question);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("修改提问成功!");
            }else{
                return ServerResponse.createByErrorMsg("修改提问失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("修改提问失败!");
        }
    }

    /**
     * 删除提问
     * @param data
     * @return
     */
    @PostMapping("/deleteQuestion")
    public ServerResponse deleteQuestion(@RequestBody String data, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        /*
        设置删除信息
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer questionId = jsonObject.getInteger("questionId");
        Question question = new Question();
        question.setQuestionId(questionId);
        question.setRoleId(roleId);
        question.setQuestionUserId(userId);
        try{
            //添加操作
            int count = questionAnswerService.delete(question);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("删除提问成功!");
            }else{
                return ServerResponse.createByErrorMsg("删除提问失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("删除提问失败!");
        }
    }

    /**
     * 添加回答
     * @param answer
     * @return
     */
    @PostMapping("/insertAnswer")
    public ServerResponse insertAnswer(@RequestBody Answer answer, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        try{
            answer.setAnswerUserId(userId);
            answer.setIfShow(0);
            //添加操作
            int count = questionAnswerService.insertAnswer(answer);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("回答成功!");
            }else{
                return ServerResponse.createByErrorMsg("回答失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("回答失败!");
        }
    }

    /**
     * 根据问题id获取回答
     * @return
     */
    @PostMapping("/getAllAnswer")
    public ServerResponse getAllAnswer(@RequestBody Answer answer, HttpServletRequest request){
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        try{
            /**
             * 判断是否是教师或管理员
             */
            if(roleId.equals(1) || roleId.equals(2)){
                List<Answer> answers = questionAnswerService.getAllAnswer(answer);
                return ServerResponse.createBySuccess("查询成功!",answers);
            }else{
                answer.setIfShow(1);
                answer.setAnswerUserId(userId);
                List<Answer> answers = questionAnswerService.getAllAnswer(answer);
                return ServerResponse.createBySuccess("查询成功!",answers);
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("查询实验报告信息错误!");
        }
    }

    /**
     * 修改回答
     * @param answer
     * @return
     */
    @PostMapping("/updateAnswer")
    public ServerResponse updateAnswer(@RequestBody Answer answer, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        answer.setAnswerUserId(userId);
        answer.setRoleId(roleId);
        try{
            //添加操作
            int count = questionAnswerService.updateAnswer(answer);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("修改回答成功!");
            }else{
                return ServerResponse.createByErrorMsg("修改回答失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("修改回答失败!");
        }
    }


    /**
     * 删除回答
     * @param data
     * @return
     */
    @PostMapping("/deleteAnswer")
    public ServerResponse deleteAnswer(@RequestBody String data, HttpServletRequest request){
        /**
         * 获取用户信息
         */
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);

        JSONObject jsonObject = JSONObject.parseObject(data);
        Long answerId = jsonObject.getLong("answerId");
        Answer answer = new Answer();
        answer.setAnswerId(answerId);
        answer.setAnswerUserId(userId);
        answer.setRoleId(roleId);
        try{
            //添加操作
            int count = questionAnswerService.deleteAnswer(answer);
            if(count > 0){
                return ServerResponse.createBySuccessMsg("删除回答成功!");
            }else{
                return ServerResponse.createByErrorMsg("删除回答失败!");
            }
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("删除回答失败!");
        }
    }
}